// pages2/pages/index/addressBook/index.js
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../../../miniprogram_npm/@vant/weapp/dialog/dialog';
import * as Storage from '../../../../utils/storage'
const $api = require("../../../../request/api").API
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		equipId: "",
		equipType: 1, //呼叫器 2 其他类型1
		list: [{
				linkName: "",
				linkPhone: "",
			},
			{
				linkName: "",
				linkPhone: "",
			},
			{
				linkName: "",
				linkPhone: "",
			},
			{
				linkName: "",
				linkPhone: "",
			},
			{
				linkName: "",
				linkPhone: "",
			},
		]
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad({
		equipId,
		equipType
	}) {
		this.setData({
			equipId,
			equipType
		})
		this.getData()
	},

	async getData() {
		let res = await $api.getSafeEquipContact({
			equipId: this.data.equipId
		})
		res = res.data.data
		if (!res || !res.linkNames) return
		let {
			linkNames,
			linkPhones,
			fastPhones
		} = res
		if (linkNames) {
			let list = this.data.list
			linkNames = linkNames.split(",")
			linkPhones = linkPhones.split(",")
			for (let i = 0; i < list.length; i++) {
				list[i].linkName = linkNames[i] == "*" ? '' : linkNames[i]
				list[i].linkPhone = linkPhones[i] == "*" ? '' : linkPhones[i]
				if (list[i].linkPhone == fastPhones) {
					list[i].flag = true
				} else {
					list[i].flag = false
				}
			}
			this.setData({
				list
			})
		}
	},

	onInput({
		currentTarget,
		detail
	}) {
		const {
			prop,
			index
		} = currentTarget.dataset
		let key = `list[${index}].${prop}`
		this.setData({
			[key]: detail.value,
		})
	},

	checkData() {
		const {
			list
		} = this.data;
		const phoneReg = /^\d+$/;
		const isValid = list.some(obj => obj.linkName!=false && obj.linkPhone!=false);
		if (isValid) {
			// 遍历每个对象，检查电话号码格式
			for (let i = 0; i < list.length; i++) {
				const obj = list[i];
				if (obj.linkPhone && !phoneReg.test(obj.linkPhone)) {
					// 如果电话号码格式不正确，返回错误信息并退出
					Toast.fail(`第${i + 1}个电话号码格式错误`);
					return false;
				}
				if (obj.linkPhone && !obj.linkName) {
					Toast.fail(`第${i + 1}个电话号码的联系人姓名未填写`);
					return false;
				}
				if (!obj.linkPhone && obj.linkName) {
					Toast.fail(`第${i + 1}个联系人的电话号码未填写`);
					return false;
				}
			}
			return true; // 如果所有数据有效且所有电话号码格式都正确，则返回 true
		} else {
			Toast.fail("至少填写一个联系人姓名和电话")
			return false; // 如果没有任何一个对象的 linkName 和 linkPhone 都有值，则返回 false
		}
	},

	onDelete(e) {
		const {
			index,
			prop
		} = e.currentTarget.dataset
		const {
			list
		} = this.data
		if (prop) {
			list[index][prop] = ""
			if (list[index].flag && prop == 'linkPhone' && list[index][prop] == '') {
				list[index].flag = false
			}
		} else {
			list.splice(index, 1)
		}
		this.setData({
			list
		})
	},

	submit() {
		if (!this.checkData()) return
		Dialog.confirm({
			title: "提示",
			message: "确定保存"
		}).then(()=>{
			Toast.loading({
        message: '请等待...',
        duration: 0
      });
			const {
				list,
				equipId
			} = this.data
			let params = {
				equipId,
				// fastPhone: ''
			};
			list.forEach((item, index) => {
				params[`${index === 0 ? 'first' : index === 1 ? 'second' : index === 2 ? 'third' : index === 3 ? 'fourth' : 'fifth'}Name`] = item.linkName;
				params[`${index === 0 ? 'first' : index === 1 ? 'second' : index === 2 ? 'third' : index === 3 ? 'fourth' : 'fifth'}Phone`] = item.linkPhone;
				// if (item.flag) {
				// 	params.fastPhone = item.linkPhone;
				// }
			});
			// if (!params.fastPhone) {
			// 	Toast.fail("请设置紧急联系人")
			// 	return false
			// }
			$api.setBeeperContact(params).then(res=>{
				if(res.data.status==0){
					Toast.success("设置成功")
				}else{
					Toast.fail(res.data.msg)
				}
			}).catch(err=>{
				Toast.fail(err.msg)
			})
		})

	},
	// setUrgencyContacts(event) {
	// 	const {
	// 		index,
	// 		flag
	// 	} = event.currentTarget.dataset
	// 	// if (!this.data.list[index].linkPhone) {
	// 	// 	Toast("当前号码为空，不能设置为紧急联系人");
	// 	// 	return false
	// 	// }

	// 	// let list = this.data.list.map((item, idx) => ({
	// 	// 	...item,
	// 	// 	flag: idx === index ? !flag : false
	// 	// }));
	// 	// if (!flag) {
	// 	// 	Toast.success("此号码已设置紧急联系人,保存后生效")
	// 	// }
	// 	this.setData({
	// 		list
	// 	});
	// }
})